home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / stdio / tst-printf.c < prev    next >
C/C++ Source or Header  |  1993-12-24  |  6KB  |  230 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #ifdef    BSD
  21. #include </usr/include/stdio.h>
  22. #define EXIT_SUCCESS 0
  23. #else
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #endif
  28.  
  29. #include <float.h>
  30.  
  31.  
  32. void
  33. DEFUN(fmtchk, (fmt), CONST char *fmt)
  34. {
  35.   (void) fputs(fmt, stdout);
  36.   (void) printf(":\t`");
  37.   (void) printf(fmt, 0x12);
  38.   (void) printf("'\n");
  39. }
  40.  
  41. void
  42. DEFUN(fmtst1chk, (fmt), CONST char *fmt)
  43. {
  44.   (void) fputs(fmt, stdout);
  45.   (void) printf(":\t`");
  46.   (void) printf(fmt, 4, 0x12);
  47.   (void) printf("'\n");
  48. }
  49.  
  50. void
  51. DEFUN(fmtst2chk, (fmt), CONST char *fmt)
  52. {
  53.   (void) fputs(fmt, stdout);
  54.   (void) printf(":\t`");
  55.   (void) printf(fmt, 4, 4, 0x12);
  56.   (void) printf("'\n");
  57. }
  58.  
  59. /* This page is covered by the following copyright: */
  60.  
  61. /* (C) Copyright C E Chew
  62.  *
  63.  * Feel free to copy, use and distribute this software provided:
  64.  *
  65.  *    1. you do not pretend that you wrote it
  66.  *    2. you leave this copyright notice intact.
  67.  */
  68.  
  69. /*
  70.  * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
  71.  */
  72.  
  73. #define DEC -123
  74. #define INT 255
  75. #define UNS (~0)
  76.  
  77. /* Formatted Output Test
  78.  *
  79.  * This exercises the output formatting code.
  80.  */
  81.  
  82. void
  83. DEFUN_VOID(fp_test)
  84. {
  85.   int i, j, k, l;
  86.   char buf[7];
  87.   char *prefix = buf;
  88.   char tp[20];
  89.  
  90.   puts("\nFormatted output test");
  91.   printf("prefix  6d      6o      6x      6X      6u\n");
  92.   strcpy(prefix, "%");
  93.   for (i = 0; i < 2; i++) {
  94.     for (j = 0; j < 2; j++) {
  95.       for (k = 0; k < 2; k++) {
  96.     for (l = 0; l < 2; l++) {
  97.       strcpy(prefix, "%");
  98.       if (i == 0) strcat(prefix, "-");
  99.       if (j == 0) strcat(prefix, "+");
  100.       if (k == 0) strcat(prefix, "#");
  101.       if (l == 0) strcat(prefix, "0");
  102.       printf("%5s |", prefix);
  103.       strcpy(tp, prefix);
  104.       strcat(tp, "6d |");
  105.       printf(tp, DEC);
  106.       strcpy(tp, prefix);
  107.       strcat(tp, "6o |");
  108.       printf(tp, INT);
  109.       strcpy(tp, prefix);
  110.       strcat(tp, "6x |");
  111.       printf(tp, INT);
  112.       strcpy(tp, prefix);
  113.       strcat(tp, "6X |");
  114.       printf(tp, INT);
  115.       strcpy(tp, prefix);
  116.       strcat(tp, "6u |");
  117.       printf(tp, UNS);
  118.       printf("\n");
  119.     }
  120.       }
  121.     }
  122.   }
  123.   printf("%10s\n", (char *) NULL);
  124.   printf("%-10s\n", (char *) NULL);
  125. }
  126.  
  127. int
  128. DEFUN_VOID(main)
  129. {
  130.   static char shortstr[] = "Hi, Z.";
  131.   static char longstr[] = "Good morning, Doctor Chandra.  This is Hal.  \
  132. I am ready for my first lesson today.";
  133.  
  134.   fmtchk("%.4x");
  135.   fmtchk("%04x");
  136.   fmtchk("%4.4x");
  137.   fmtchk("%04.4x");
  138.   fmtchk("%4.3x");
  139.   fmtchk("%04.3x");
  140.  
  141.   fmtst1chk("%.*x");
  142.   fmtst1chk("%0*x");
  143.   fmtst2chk("%*.*x");
  144.   fmtst2chk("%0*.*x");
  145.  
  146. #ifndef    BSD
  147.   printf("bad format:\t\"%z\"\n");
  148.   printf("nil pointer (padded):\t\"%10p\"\n", (PTR) NULL);
  149. #endif
  150.  
  151.   printf("decimal negative:\t\"%d\"\n", -2345);
  152.   printf("octal negative:\t\"%o\"\n", -2345);
  153.   printf("hex negative:\t\"%x\"\n", -2345);
  154.   printf("long decimal number:\t\"%ld\"\n", -123456L);
  155.   printf("long octal negative:\t\"%lo\"\n", -2345L);
  156.   printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
  157.   printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
  158.   printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
  159.   printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
  160.   printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
  161.  
  162.   printf("zero-padded string:\t\"%010s\"\n", shortstr);
  163.   printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
  164.   printf("space-padded string:\t\"%10s\"\n", shortstr);
  165.   printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
  166.   printf("null string:\t\"%s\"\n", (char *)NULL);
  167.   printf("limited string:\t\"%.22s\"\n", longstr);
  168.  
  169.   printf("e-style >= 1:\t\"%e\"\n", 12.34);
  170.   printf("e-style >= .1:\t\"%e\"\n", 0.1234);
  171.   printf("e-style < .1:\t\"%e\"\n", 0.001234);
  172.   printf("e-style big:\t\"%.60e\"\n", 1e20);
  173.   printf ("e-style == .1:\t\"%e\"\n", 0.1);
  174.   printf("f-style >= 1:\t\"%f\"\n", 12.34);
  175.   printf("f-style >= .1:\t\"%f\"\n", 0.1234);
  176.   printf("f-style < .1:\t\"%f\"\n", 0.001234);
  177.   printf("g-style >= 1:\t\"%g\"\n", 12.34);
  178.   printf("g-style >= .1:\t\"%g\"\n", 0.1234);
  179.   printf("g-style < .1:\t\"%g\"\n", 0.001234);
  180.   printf("g-style big:\t\"%.60g\"\n", 1e20);
  181.  
  182.   printf (" %6.5f\n", .099999999860301614);
  183.   printf (" %6.5f\n", .1);
  184.   printf ("x%5.4fx\n", .5);
  185.  
  186.   printf ("%#03x\n", 1);
  187.  
  188.  
  189.   {
  190.     double d = FLT_MIN;
  191.     int niter = 17;
  192.     
  193.     while (niter-- != 0)
  194.       printf ("%.17e\n", d / 2);
  195.     fflush (stdout);
  196.   }
  197.  
  198.   printf ("%15.5e\n", 4.9406564584124654e-324);
  199.  
  200. #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
  201.   printf (FORMAT, 0.0, 0.0, 0.0);
  202.   printf (FORMAT, 1.0, 1.0, 1.0);
  203.   printf (FORMAT, -1.0, -1.0, -1.0);
  204.   printf (FORMAT, 100.0, 100.0, 100.0);
  205.   printf (FORMAT, 1000.0, 1000.0, 1000.0);
  206.   printf (FORMAT, 10000.0, 10000.0, 10000.0);
  207.   printf (FORMAT, 12345.0, 12345.0, 12345.0);
  208.   printf (FORMAT, 100000.0, 100000.0, 100000.0);
  209.   printf (FORMAT, 123456.0, 123456.0, 123456.0);
  210. #undef    FORMAT
  211.  
  212.   {
  213.     char buf[20];
  214.     printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
  215.         snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
  216.   }
  217.  
  218.   fp_test ();
  219.  
  220.   printf ("%e should be 1.234568e+06\n", 1234567.8);
  221.   printf ("%f should be 1234567.800000\n", 1234567.8);
  222.   printf ("%g should be 1.23457e+06\n", 1234567.8);
  223.   printf ("%g should be 123.456\n", 123.456);
  224.   printf ("%g should be 1e+06\n", 1000000.0);
  225.   printf ("%g should be 10\n", 10.0);
  226.   printf ("%g should be 0.02\n", 0.02);
  227.  
  228.   exit(EXIT_SUCCESS);
  229. }
  230.